package com.thejupitech;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.view.View.OnClickListener;


public class BookSearchActivity extends Activity {
	private ProgressDialog progDialog;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_layout);
        
        final EditText edtSearch = (EditText) findViewById(R.id.search_text);
        final Button btnSearch = (Button) findViewById(R.id.search_btn);
 
        btnSearch.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String strKey = edtSearch.getText().toString().trim();
				strKey = strKey.replaceAll("\\W", "");
				if(!strKey.equals("")) {				
					Bundle sendBundle = new Bundle();
					sendBundle.putString("key", strKey);				
					new LoadingData().execute(sendBundle);			
				} else {
					new AlertDialog.Builder(BookSearchActivity.this).setTitle("Information!")
					.setMessage("Text field cannot be empty!!!")
					.setNegativeButton("Close", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}						
						
					}).show();
				}			
			}			
			
		});

    }
    
    
    private class LoadingData extends AsyncTask<Bundle, Void, Void> {
		@Override
		protected void onPreExecute() {
			progDialog = ProgressDialog.show(BookSearchActivity.this, "Getting data", "Loading...");
		}
		@Override
		protected Void doInBackground(Bundle... params) {
			Intent i = new Intent(BookSearchActivity.this, BooksActivity.class);
			i.putExtras(params[0]);
			startActivity(i);
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			progDialog.dismiss();
			finish();
		}
    	
    }
}